package com.eascs.o2o.uc.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.o2o.common.security.MD5;
import com.eascs.o2o.common.vo.ModelResult;
import com.eascs.o2o.operateLog.server.OperateLogService;
import com.eascs.o2o.uc.entity.MemberOperateLogEntity;
import com.eascs.o2o.uc.entity.UserEntity;
import com.eascs.o2o.uc.service.UserService;
import com.eascs.o2o.uc.utils.CusAccessObjectUtil;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;

@RestController
@RequestMapping("/uc/rest")
public class UserRestController extends BaseRestController {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	UserService userService;
	@Autowired
	OperateLogService operateLogService;
	
	public static void setSession(HttpServletRequest request,UserEntity userEntity){
		HttpSession session = request.getSession();
		String sessionId=session.getId();
		session.removeAttribute(sessionId);
		session.setAttribute(sessionId, userEntity);
	}
	public static ModelResult<UserEntity> isLogin(HttpServletRequest request) {
		ModelResult<UserEntity> result = new ModelResult<>();
		try {
			HttpSession session = request.getSession();
			Object obj = session.getAttribute(session.getId());
			if (obj != null) {
				result.withModel((UserEntity) obj);
			}
		} catch (Exception e) {
			result.withError("LoginRestController.isLogin", "session获取登陆用户失败");
			e.printStackTrace();
		}
		return result;
	}
	
	@RequestMapping(value = "/login")
	@ResponseBody
	public BaseRestResult<ModelResult<Boolean>> alertPassword(HttpServletRequest request, String mobile, String password) {
		ModelResult<Boolean> result=new ModelResult<>();
		if (mobile.isEmpty() || password.isEmpty()) {
			result.withError("param is null", "登陆参数不能为空");
			return BaseRestResult.buildRestResult(result);
		}
		String pw=MD5.md5Encode(password.trim());
		UserEntity entity = new UserEntity(mobile,pw);
		ModelResult<UserEntity> modelResult = userService.queryUserEntity(entity);
		UserEntity userModel = modelResult.getModel();
		if (userModel!= null) {
			try {
				//用户日志
				String ip=CusAccessObjectUtil.getIpAddress(request);
				MemberOperateLogEntity logEntity=new MemberOperateLogEntity();
				logEntity.setCreateTime(new Date());
				logEntity.setOperater(userModel.getAccount());
				logEntity.setLocation(ip);
				logEntity.setOperateDetail(request.getRequestURL().toString());
				operateLogService.insertMemberOperateLog(logEntity);
			} catch (Exception e) {
				logger.info("request.controller.error:/uc/login,exception:{}", e.getMessage());
			}
			UserRestController.setSession(request, userModel);
			result.withModel(true);
			return BaseRestResult.buildRestResult(result);
		}
		result.withError("param error", "账号或密码不正确");		
		return BaseRestResult.buildRestResult(result);
	}
	
	@RequestMapping(value = "/register")
	@ResponseBody
	public BaseRestResult<ModelResult<Boolean>> register(HttpServletRequest request, String mobile, String password) {
		ModelResult<Boolean> result=new ModelResult<>();
		if (mobile.isEmpty() || password.isEmpty()) {
			result.withError("param is null", "登陆参数不能为空");
			return BaseRestResult.buildRestResult(result);
		}
		UserEntity member=new UserEntity();
		member.setAccount(mobile);
		member.setMobile(mobile);
		String pw=MD5.md5Encode(password.trim());
		ModelResult<UserEntity> queryResult=userService.queryUserEntity(member);
		if (!queryResult.isSuccess() || queryResult.getModel() != null) {
			result.withError("user is exist", "手机号码已注册");
			return BaseRestResult.buildRestResult(result);
		}
		member.setPassword(pw);
		ModelResult<UserEntity> modelResult = userService.insertUserEntity(member);
		if (modelResult.isSuccess() && modelResult.getModel()!=null) {
			UserEntity model=modelResult.getModel();
			if (model != null) {
				UserRestController.setSession(request,member);
				result.withModel(true);
				return BaseRestResult.buildRestResult(result);
			}
		}
		result.withError("register error", "注册失败");
		return BaseRestResult.buildRestResult(result);
	}	
	
}
